<template>
  <el-form ref="auditActor" :model="actor" label-width="150px" :rules="actorRules">
    <el-form-item label="限定范围" prop="Mode">
      <el-radio-group v-model="actor.Mode" :disabled="readonly" class="specifyMode" @change="modeChange">
        <el-radio v-for="item in WhileSpecifyMode" :key="item.value" :label="item.value">{{ item.text }}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="筛选条件">
      <el-radio-group v-model="actor.TitleOrPost" :disabled="readonly">
        <el-radio v-if="actor.Mode !=2" :label="0">职务</el-radio>
        <el-radio v-if="actor.Mode !=2" :label="1">岗位</el-radio>
        <el-radio :label="2">负责人</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item v-if="actor.TitleOrPost == 0" label="限定职务" prop="TitleCode">
      <dictItem
        v-model="actor.TitleCode"
        :readonly="readonly"
        :dic-id="HrItemDic.title"
        :multiple="true"
        :label.sync="actor.TitleName"
        placeholder="职务"
      />
    </el-form-item>
    <el-form-item v-if="actor.TitleOrPost == 1" label="限定岗位" prop="PostCode">
      <treeDicItem
        v-model="actor.PostCode"
        :readonly="readonly"
        :dic-id="HrItemDic.post"
        :is-multiple="true"
        :label.sync="actor.PostName"
        placeholder="岗位"
      />
    </el-form-item>
    <div v-if="readonly == false" style="text-align: center">
      <el-button type="primary" @click="saveActor">保存</el-button>
      <el-button @click="intAuditEmp">重置</el-button>
    </div>
  </el-form>
</template>

<script>
import { WhileSpecifyMode } from '@/flow/config/flowConfig'
import { HrItemDic } from '@/config/publicDic'
import * as nodeAuditEmpApi from '@/flow/api/flowNodeAuditEmp'

export default {
  components: {
  },
  props: {
    nodeId: {
      type: String,
      default: null
    },
    flowId: {
      type: String,
      default: null
    },
    readonly: {
      type: Boolean,
      default: false
    },
    isLoad: {
      type: Boolean,
      default: false
    },
    show: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      HrItemDic,
      WhileSpecifyMode,
      actorRules: {
        Mode: [
          {
            required: true,
            message: '限定范围不能为空！',
            trigger: 'blur'
          }
        ],
        UnitId: [
          {
            required: true,
            message: '限定单位不能为空！',
            trigger: 'blur'
          }
        ],
        DeptId: [
          {
            required: true,
            message: '限定单位不能为空！',
            trigger: 'blur'
          }
        ],
        EmpId: [
          {
            required: true,
            message: '指定人员不能为空！',
            trigger: 'blur'
          }
        ],
        CompanyId: [
          {
            required: true,
            message: '限定公司不能为空！',
            trigger: 'blur'
          }
        ],
        TitleCode: [
          {
            required: true,
            message: '职务不能为空！',
            trigger: 'blur'
          }
        ],
        PostCode: [
          {
            required: true,
            message: '岗位不能为空！',
            trigger: 'blur'
          }
        ],
        ToNodeId: [
          {
            required: true,
            message: '指定节点不能为空！',
            trigger: 'blur'
          }
        ]
      },
      actor: {
        TitleOrPost: 0,
        Mode: 0,
        TitleCode: null,
        PostCode: null,
        Show: null
      }
    }
  },
  computed: {
    comId() {
      return this.$store.getters.curComId
    }
  },
  watch: {
    isLoad: {
      handler(val) {
        if (val) {
          this.intAuditEmp()
        }
      },
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    modeChange() {
      this.actor.TitleOrPost = this.actor.Mode !== 2 ? 0 : 2
      this.actor.TitleCode = null
      this.actor.PostCode = null
    },
    saveActor() {
      const that = this
      this.$refs['auditActor'].validate((valid) => {
        if (valid) {
          that.setActor()
        } else {
          return false
        }
      })
    },
    formatActor() {
      const data = {
        FlowId: this.flowId,
        NodeId: this.nodeId
      }
      const mode = this.actor.Mode
      data.CompanyId = this.comId
      data.TitleCode = this.actor.TitleCode
      data.PostCode = this.actor.PostCode
      if (mode === 0 && this.actor.TitleOrPost === 0) {
        data.Mode = 20
        data.Show = '逐级部门，职务:' + this.actor.TitleName
      } else if (mode === 0 && this.actor.TitleOrPost === 1) {
        data.Mode = 12
        data.Show = '逐级部门，岗位:' + this.actor.PostName
      } else if (mode === 0 && this.actor.TitleOrPost === 2) {
        data.Mode = 68
        data.Show = '逐级部门，负责人'
      } else if (mode === 1 && this.actor.TitleOrPost === 0) {
        data.Mode = 48
        data.Show = '逐级单位，职务:' + this.actor.TitleName
      } else if (mode === 1 && this.actor.TitleOrPost === 1) {
        data.Mode = 40
        data.Show = '逐级单位，岗位:' + this.actor.PostName
      } else if (mode === 1 && this.actor.TitleOrPost === 2) {
        data.Mode = 96
        data.Show = '逐级单位，负责人'
      } else if (mode === 2 && this.actor.TitleOrPost === 0) {
        data.Mode = 52
        data.Show = '逐级单位和部门，职务:' + this.actor.TitleName
      } else if (mode === 2 && this.actor.TitleOrPost === 1) {
        data.Mode = 44
        data.Show = '逐级单位和部门，岗位:' + this.actor.PostName
      } else if (mode === 2 && this.actor.TitleOrPost === 2) {
        data.Mode = 100
        data.Show = '逐级单位和部门，负责人'
      }
      return data
    },
    async setActor() {
      const data = this.formatActor()
      await nodeAuditEmpApi.Set(data)
      this.$message({
        type: 'success',
        message: '保存成功!'
      })
      this.$emit('update:show', data.Show)
      this.$emit('change', data)
    },
    async intAuditEmp() {
      const res = await nodeAuditEmpApi.Get(this.nodeId)
      if (res == null) {
        this.actor.Mode = 0
        this.modeChange()
      } else {
        this.actor.CompanyId = this.comId
        this.actor.TitleCode = res.TitleCode
        this.actor.PostCode = res.PostCode
        if ((36 & res.Mode) === 36) {
          this.actor.Mode = 2
        } else if ((4 & res.Mode) === 4) {
          this.actor.Mode = 0
        } else {
          this.actor.Mode = 1
        }
        if ((64 & res.Mode) === 64) {
          this.actor.TitleOrPost = 2
        } else if ((16 & res.Mode) === 16) {
          this.actor.TitleOrPost = 0
        } else if ((8 & res.SpecifyMode) === 8) {
          this.actor.TitleOrPost = 1
        }
      }
    }
  }
}
</script>
<style scoped>
.el-checkbox-group {
    line-height: 40px
}
.specifyMode .el-radio {
  line-height: 20px;
}
</style>
